package com.jett.JFtpServer.Utils;

import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.jett.JFtpServer.Enum.RetMsg;

import java.nio.charset.StandardCharsets;

public class SM4Util {
    private SymmetricCrypto sm4;

    public SM4Util() {
        this.sm4 = SmUtil.sm4();
    }

    public SM4Util(String key) {
        if (key != null && key.trim().length() != 0) {
            this.sm4 = SmUtil.sm4(key.getBytes(StandardCharsets.UTF_8));
        } else {
            throw new RuntimeException(RetMsg.Err0006.getRetMsg());
        }
    }

    public String decryptStr(String data, boolean requireFlag, RetMsg retMsg) {
        if (requireFlag) {
            if (data != null && data.trim().length() != 0) {
                return this.sm4.decryptStr(data);
            } else {
                throw new RuntimeException(retMsg.getRetMsg());
            }
        } else {
            return data != null && data.trim().length() != 0 ? this.sm4.decryptStr(data) : "";
        }
    }
    public String encryptHex(String data){
       return sm4.encryptHex(data);
    }
}
